<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formAdTrait{
    public function index_ad(Content $content)
    {
        return $content->title('广告配置')->description('小程序平台展示小程序广告，其他平台展示Dcloud广告，所以每个位置都是需要配置两份，<b>填的均是广告id</b>，微信广告如：adunit-0bf9d9799d0ccd64')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_ad()->render()}</div>
HTML
            );
    }
    protected function form_ad(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        global $__MINI_GLOBAL_TENANT_ID__;
        $form->tab('详情', function (Form $form) {

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">广告位置一：笔记内容底部</div>';
            }, '');
            $form->html(function () {
                $res14 = __system__paycode__tip__(14);
                Tooltip::make('.mini_global_admin_open_not_support_tip')
                    ->bottom()
                    ->title($res14['tip']);
                return $res14['text'];
            }, '<b>1、微信小程序广告</b>');

            $form->table('ad_mp_words_sticky','笔记详情页', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID')->help('仅支持“原生模板广告”类型');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_mp_words_sticky', '', true));

            $form->table('ad_mp_single_sticky','图文详情页', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID')->help('仅支持“原生模板广告”类型');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_mp_single_sticky', '', true));

            $form->table('ad_mp_video_sticky','视频详情页', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID')->help('仅支持“原生模板广告”类型');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_mp_video_sticky', '', true));

            $form->html(function () {
                return '';
            }, '<b>2、Dcloud广告</b>');

            $form->table('ad_uni_words_sticky','笔记详情页', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_uni_words_sticky', '', true));

            $form->table('ad_uni_single_sticky','图文详情页', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_uni_single_sticky', '', true));

            $form->table('ad_uni_video_sticky','视频详情页', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_uni_video_sticky', '', true));

        });
        $form->tab('笔记列表页推流', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">广告位置二：笔记推流</div>';
            }, '');
            $form->html(function () {
                $res14 = __system__paycode__tip__(14);
                Tooltip::make('.mini_global_admin_open_not_support_tip')
                    ->bottom()
                    ->title($res14['tip']);
                return $res14['text'];
            }, '<b>1、微信小程序广告</b>');

            $form->table('ad_mp_push_flow','笔记推流', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID')->help('仅支持“原生模板广告”类型');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_mp_push_flow', '', true));

            $form->html(function () {
                return '';
            }, '<b>2、Dcloud广告</b>');

            $form->table('ad_uni_push_flow','笔记推流', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_uni_push_flow', '', true));

        });
        $form->tab('评论推流', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">广告位置三：评论推流</div>';
            }, '');
            $form->html(function () {
                return '';
            }, '<b>1、微信小程序广告</b>');

            $form->table('ad_mp_comment_push_flow','评论推流', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID')->help('仅支持“原生模板广告”类型');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_mp_comment_push_flow', '', true));

            $form->html(function () {
                return '';
            }, '<b>2、Dcloud广告</b>');
            $form->table('ad_uni_comment_push_flow','评论推流', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_uni_comment_push_flow', '', true));


        });
        $form->tab('瀑布流推流', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">广告位置四：瀑布流推流</div>';
            }, '');
            $form->html(function () {
                return '';
            }, '<b>1、微信小程序广告</b>');
            $form->table('ad_mp_water_flow','瀑布流', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID')->help('仅支持“原生模板广告”类型');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_mp_water_flow', '', true));

            $form->html(function () {
                return '';
            }, '<b>2、Dcloud广告</b>');
            $form->table('ad_uni_water_flow','瀑布流', function (NestedForm $table) {
                $table->radio('type', '类型')->options([0=>'平台广告', 1=>'图片广告'])->default(0);
                $table->text('unit', '广告ID');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/ad/addefault.png" download="about_logo.jpg" target="_blank">点击下载默认图</a>，最好是png透明图')->url('files/uploads')->autoUpload()->uniqueName()->default('https://img.mini.minisns.cn/ad/addefault.png');
                $table->radio('target_type', '跳转类型（图片类型时）')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id（图片类型时）')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            })->help('配置多个则是随机出现')->default(Settings::get('ad_uni_water_flow', '', true));

        });

        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            $form->tab('激励广告', function (Form $form) {
                $form->html(function () {
                    return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">广告位置五：金币支付不足时，弹出激励视频奖励，或者在“我的钱包”点击硬币进入该页面</div>';
                }, '');
                $form->image('ad_reward_inspire_img', '激励页面的gif图')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('ad_reward_inspire_img', 'https://img.mini.minisns.cn/images/inspire-face.png'));
                $form->divider();
                $form->number('ad_mp_reward_every_day_times', '每天观看次数上限（小程序）')->default(Settings::get('ad_mp_reward_every_day_times', 10, true));
                $form->number('ad_mp_reward_every', '奖励金币数量（小程序）')->help('每次观看完激励视频后，可以获得多少金币')->default(Settings::get('ad_mp_reward_every', 20, true));
                $form->divider();
                $form->number('ad_uni_reward_every_day_times', '每天观看次数上限（Dcloud）')->default(Settings::get('ad_uni_reward_every_day_times', 10, true));
                $form->number('ad_uni_reward_every', '奖励金币数量（Dcloud）')->help('每次观看完激励视频后，可以获得多少金币')->default(Settings::get('ad_uni_reward_every', 20, true));
                $form->html(function () {
                    return '';
                }, '<b>1、微信小程序广告</b>');
                $form->text('ad_mp_reward_unit', '激励视频广告')->help('请选择“激励视频”类型的广告，在进行金币支付时，若金币不足，则弹窗提示是否观看激励视频获取奖励。')->default(Settings::get('ad_mp_reward_unit','', true));
                $form->html(function () {
                    return '';
                }, '<b>2、Dcloud广告</b>');
                $form->text('ad_uni_reward_unit', '激励视频广告')->default(Settings::get('ad_uni_reward_unit','', true));
            });
            $form->tab('视频前贴片', function (Form $form) {
                $form->html(function () {
                    return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">广告位置六：视频前贴片广告，仅支持小程序</div>';
                }, '');
                $form->text('ad_mp_video_pre_sticker', '视频前贴片广告')->default(Settings::get('ad_mp_video_pre_sticker', '', true));
            });
        }

        $form->action('settings/action/ad');
        return $form;
    }

    public function ad(Request $request){
        SiteUtils::forget_config_data();
        global $__MINI_GLOBAL_TENANT_ID__;

        Settings::set('ad_mp_words_sticky', AdminUtils::_table_data($request->input('ad_mp_words_sticky')), true, true);
        Settings::set('ad_mp_single_sticky', AdminUtils::_table_data($request->input('ad_mp_single_sticky')), true, true);
        Settings::set('ad_mp_video_sticky', AdminUtils::_table_data($request->input('ad_mp_video_sticky')), true, true);

        Settings::set('ad_uni_words_sticky', AdminUtils::_table_data($request->input('ad_uni_words_sticky')), true, true);
        Settings::set('ad_uni_single_sticky', AdminUtils::_table_data($request->input('ad_uni_single_sticky')), true, true);
        Settings::set('ad_uni_video_sticky', AdminUtils::_table_data($request->input('ad_uni_video_sticky')), true, true);

        Settings::set('ad_mp_push_flow', AdminUtils::_table_data($request->input('ad_mp_push_flow')), true, true);
        Settings::set('ad_uni_push_flow', AdminUtils::_table_data($request->input('ad_uni_push_flow')), true, true);

        Settings::set('ad_mp_comment_push_flow', AdminUtils::_table_data($request->input('ad_mp_comment_push_flow')), true, true);
        Settings::set('ad_uni_comment_push_flow', AdminUtils::_table_data($request->input('ad_uni_comment_push_flow')), true, true);

        Settings::set('ad_mp_water_flow', AdminUtils::_table_data($request->input('ad_mp_water_flow')), true, true);
        Settings::set('ad_uni_water_flow', AdminUtils::_table_data($request->input('ad_uni_water_flow')), true, true);

        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            Settings::set('ad_mp_reward_unit', $request->input('ad_mp_reward_unit'), true, true);
            Settings::set('ad_uni_reward_unit', $request->input('ad_uni_reward_unit'), true, true);

            Settings::set('ad_mp_reward_every', $request->input('ad_mp_reward_every'), true, true);
            Settings::set('ad_uni_reward_every', $request->input('ad_uni_reward_every'), true, true);

            Settings::set('ad_reward_inspire_img', $request->input('ad_reward_inspire_img'), true, true);
            Settings::set('ad_mp_reward_every_day_times', $request->input('ad_mp_reward_every_day_times'), true, true);
            Settings::set('ad_uni_reward_every_day_times', $request->input('ad_uni_reward_every_day_times'), true, true);

            Settings::set('ad_mp_video_pre_sticker', $request->input('ad_mp_video_pre_sticker'), true, true);
        }

        Cache::forget(_multi_key('ad_config'));
        $ad_config = [
            'mp' => [
                'position1' => [
                    "words" => json_decode(AdminUtils::_table_data($request->input('ad_mp_words_sticky', '[]')), true),
                    "single" => json_decode(AdminUtils::_table_data($request->input('ad_mp_single_sticky', '[]')), true),
                    "video" => json_decode(AdminUtils::_table_data($request->input('ad_mp_video_sticky', '[]')), true),
                ],
                'position2' => [
                    "flow" => json_decode(AdminUtils::_table_data($request->input('ad_mp_push_flow', '[]')), true),
                ],
                'position3' => [
                    'flow' => json_decode(AdminUtils::_table_data($request->input('ad_mp_comment_push_flow', '[]')), true),
                ],
                'position4' => [
                    'flow' => json_decode(AdminUtils::_table_data($request->input('ad_mp_water_flow', '[]')), true),
                ],
                'position5' => [
                    'reward' => Settings::get('ad_mp_reward_unit', ''),
                ],
                'position6' => [
                    'pre_sticker' => Settings::get('ad_mp_video_pre_sticker', ''),
                ]
            ],
            'uni' => [
                'position1' => [
                    "words" => json_decode(AdminUtils::_table_data($request->input('ad_uni_words_sticky', '[]')), true),
                    "single" => json_decode(AdminUtils::_table_data($request->input('ad_uni_single_sticky', '[]')), true),
                    "video" => json_decode(AdminUtils::_table_data($request->input('ad_uni_video_sticky', '[]')), true),
                ],
                'position2' => [
                    "flow" => json_decode(AdminUtils::_table_data($request->input('ad_uni_push_flow', '[]')), true),
                ],
                'position3' => [
                    'flow' => json_decode(AdminUtils::_table_data($request->input('ad_uni_comment_push_flow', '[]')), true),
                ],
                'position4' => [
                    'flow' => json_decode(AdminUtils::_table_data($request->input('ad_uni_water_flow', '[]')), true),
                ],
                'position5' => [
                    'reward' => Settings::get('ad_uni_reward_unit', '')
                ],
                'position6' => [
                    'pre_sticker' => null
                ]
            ]
        ];
        Cache::put( _multi_key('ad_config'), json_encode($ad_config));



        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
